using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace GearBlog.Web.Helpers
{
	using System.Collections;
	using System.Text;
	using Castle.MonoRail.Framework.Helpers;
	using GearBlog.Web.Utils;


	public class FCKEditHelper:AbstractHelper
	{
		public string InstallScripts()
		{
			return IncludeHelper.JavaScript("/fckeditor/fckeditor", CurrentContext);
		}

		public string InstallReplaceTextArea(string fieldName, bool isInline, IDictionary parameters)
		{
			StringBuilder scriptBuilder=new StringBuilder();
			scriptBuilder.AppendFormat(
				@"    
		var oFCKeditor = new FCKeditor('{0}') ;
		oFCKeditor.BasePath = '{1}' ;", fieldName, IncludeHelper.GetPath("/fckeditor/", "", CurrentContext)
				);
			scriptBuilder.AppendLine();
			ExtendedSetup(scriptBuilder, parameters);

			scriptBuilder.AppendLine("oFCKeditor.ReplaceTextarea() ;");
			string script;
			if (!isInline)
				script =
					string.Format(
						@"
<script type='text/javascript'>
  window.onload = function()
  {{
  	{0}
  }}
</script>
", scriptBuilder);
			else
				script = scriptBuilder.ToString();
			return script;
		}

		private static void ExtendedSetup(StringBuilder scriptBuilder, IDictionary parameters)
		{
			if (parameters != null)
			{
				int width = 0, height = 0;
				if (int.TryParse(DictUtil.ObtainEntryAndRemove(parameters, "width", ""), out width))
					scriptBuilder.AppendFormat("oFCKeditor.Width={0};\n", width);

				if (int.TryParse(DictUtil.ObtainEntryAndRemove(parameters, "height", ""), out height))
					scriptBuilder.AppendFormat("oFCKeditor.Height={0};\n", height);

				string toolbarSet = DictUtil.ObtainEntryAndRemove(parameters, "toolbarSet", "");
				if (!string.IsNullOrEmpty(toolbarSet))
					scriptBuilder.AppendFormat("oFCKeditor.ToolbarSet='{0}';\n", toolbarSet);

				string imageBrowserUrl = DictUtil.ObtainEntryAndRemove(parameters, "imageBrowserUrl", "");
				if (!string.IsNullOrEmpty(imageBrowserUrl))
					scriptBuilder.AppendFormat("oFCKeditor.Config.ImageBrowserURL='{0}';\n", imageBrowserUrl);

				string removeImageUpload = DictUtil.ObtainEntryAndRemove(parameters, "removeImageUpload", "");
				if (removeImageUpload=="true")
				{
					parameters.Remove("removeImageUpload");
					scriptBuilder.AppendLine("oFCKeditor.Config.ImageUpload=false;");
				}

			}

		}

		public string InstallInline(string fieldName, bool withScriptTag, IDictionary parameters)
		{

			StringBuilder scriptBuilder=new StringBuilder();
			scriptBuilder.AppendFormat(
	@"
	  var oFCKeditor = new FCKeditor('{0}');
	  oFCKeditor.BasePath = '{1}';
	",
				fieldName, IncludeHelper.GetPath("/fckeditor/", "", CurrentContext)
				);
			scriptBuilder.AppendLine();
			ExtendedSetup(scriptBuilder, parameters);

			scriptBuilder.AppendLine("oFCKeditor.Create();");
			string script;
			if (withScriptTag)
				script=string.Format("<script type='text/javascript'>{0}</script>", scriptBuilder);
			else
				script = scriptBuilder.ToString();
			return script;

		}
	}
}
